package com.cskaoyan.tcp.edition2;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/*
        服务器端
 */
public class Server {

    public static void main(String[] args) throws IOException {

        // 1. 创建ServerSocket对象
        ServerSocket serverSocket = new ServerSocket(9090);

        // 2. accept 接收处理连接请求
        Socket socket = serverSocket.accept();

        //3. 获取输入流，读取发送端发送的数据
        InputStream in = socket.getInputStream();
        byte[] bytes = new byte[1024];
        // 接收数据
        int count = in.read(bytes);

        // 解析读取到的数据
        String result = new String(bytes, 0, count);
        System.out.println(result);

        // 发送响应
        //  获取输出流
        OutputStream out = socket.getOutputStream();
        String response = "接收到了数据: " + result;
        // 发送响应数据
        out.write(response.getBytes());

        // 关闭socket
        socket.close();
        serverSocket.close();

    }
}
